package com.whj.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.whj.config.AlipayTemplate;

import com.whj.entity.Cart;
import com.whj.entity.SysUser;
import com.whj.service.CartService;
import com.whj.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付宝接口
 */
@RestController
@RequestMapping("/alipay")
public class AliPayController {

    //扫码支付模板
//    @Resource
//    AlipayTemplate alipayTemplate;

    //账号密码支付模板
    @Autowired
    AlipayTemplate alipayTemplate;
    @Autowired
    CartService cartService;
    @Autowired
    UserService userService;


    @GetMapping(value = "/pay", produces = "text/html;charset=utf-8")
    public String pay(Integer userId, HttpSession session) throws AlipayApiException, IOException {
        List<Cart> carts = cartService.selectListById(userId);
        SysUser user = userService.selectUserById(userId);
        session.setAttribute("carts", carts);
        cartService.deleteCart(userId);
        return alipayTemplate.pay(carts,user);
    }
    @PostMapping("/notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request) throws Exception {
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            System.out.println("=========支付宝异步回调========");

            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
                // System.out.println(name + " = " + request.getParameter(name));
            }

            String tradeNo = params.get("out_trade_no");
            String gmtPayment = params.get("gmt_payment");
            String alipayTradeNo = params.get("trade_no");
            // 支付宝验签

            boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayTemplate.alipayPublicKey, alipayTemplate.getCharset(), alipayTemplate.getSignType()); //调用SDK验证签名
            System.out.println(signVerified);
            if (signVerified) {
                // 验签通过
                System.out.println("交易名称: " + params.get("subject"));
                System.out.println("交易状态: " + params.get("trade_status"));
                System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
                System.out.println("商户订单号: " + params.get("out_trade_no"));
                System.out.println("交易金额: " + params.get("total_amount"));
                System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
                System.out.println("买家付款时间: " + params.get("gmt_payment"));
                System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
                // 更新订单状态
            }
        }
        return "success";
    }


}


